﻿#region Đào Mạnh Hòa
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Collections;
using System.Transactions;

namespace BusinessLogic
{

    public static class SoQuy
    {

        static SqlCommand cmd;
        static ArrayList arr;
        static SqlDataReader dr;
        static SqlConnection cn;
        #region select
        public static Entities.SoDuSoQuy[] Select(string makho)
        {
            try
            {
                string sql = Common.Constants.SoQuy.SelectSoDuSoQuy;
                cn = DataAccess.Connection.OpenConnection();
                cmd = new SqlCommand(sql, cn);
                cmd.Parameters.Add(Common.Constants.SoQuy.MaKho, SqlDbType.VarChar, 50).Value = makho;
                dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                arr = new ArrayList();
                while (dr.Read())
                {

                    Entities.SoDuSoQuy sodusoquy = new Entities.SoDuSoQuy();
                    sodusoquy.SoDuSoQuyID = Convert.ToInt32(dr[Common.Constants.SoQuy.SoDuSoQuyID].ToString());
                    sodusoquy.MaSoDuSoQuy = dr[Common.Constants.SoQuy.MaSoDuSoQuy].ToString();
                    sodusoquy.SoDuDauKy = dr[Common.Constants.SoQuy.SoDuDauKy].ToString();
                    sodusoquy.NgayKetChuyen = Convert.ToDateTime(dr[Common.Constants.SoQuy.NgayKetChuyen].ToString());
                    sodusoquy.SoDuCuoiKy = dr[Common.Constants.SoQuy.SoDuCuoiKy].ToString();
                    sodusoquy.MaKho = dr[Common.Constants.SoQuy.MaKho].ToString();
                    sodusoquy.TrangThai = Boolean.Parse(dr[Common.Constants.SoQuy.TrangThai].ToString());
                    arr.Add(sodusoquy);
                }
                return (Entities.SoDuSoQuy[])arr.ToArray(typeof(Entities.SoDuSoQuy));
            }
            catch
            {
                return new Entities.SoDuSoQuy[0];
            }
        }
        #endregion

        #region get id
        public static string getID()
        {
            try
            {
                string str = "";
                Entities.GetID GetID = new Entities.GetID();
                GetID.TableName = "SoDuSoQuy";
                Entities.GetID lay = BusinessLogic.GetID.SelectID(GetID);
                if (lay.ColumnName == null)
                {
                    return "SDSQ_0001";
                }
                str = Common.Utilities.ProcessID(lay.ColumnName);
                return str;
            }
            catch
            {
                return "SDSQ_0001";
            }
        }
        #endregion

        #region insert
        public static int Insert(Entities.SoDuSoQuy pxh)
        {
            TransactionScope ts = null;
            int rt = 0;
            try
            {
                using (ts = new TransactionScope(TransactionScopeOption.Required))
                {
                    string sql = Common.Constants.SoQuy.InsertSoDuSoQuy;
                    cn = DataAccess.Connection.OpenConnection();
                    SqlCommand cmd = new SqlCommand(sql, cn);
                    cmd.Parameters.Add(Common.Constants.SoQuy.MaSoDuSoQuy, SqlDbType.VarChar, 50).Value = getID();
                    cmd.Parameters.Add(Common.Constants.SoQuy.SoDuDauKy, SqlDbType.Float).Value = pxh.SoDuDauKy;
                    cmd.Parameters.Add(Common.Constants.SoQuy.NgayKetChuyen, SqlDbType.DateTime).Value = pxh.NgayKetChuyen;
                    cmd.Parameters.Add(Common.Constants.SoQuy.SoDuCuoiKy, SqlDbType.Float).Value = pxh.SoDuCuoiKy;
                    cmd.Parameters.Add(Common.Constants.SoQuy.MaKho, SqlDbType.VarChar, 50).Value = pxh.MaKho;
                    cmd.Parameters.Add(Common.Constants.SoQuy.TrangThai, SqlDbType.Bit).Value = pxh.TrangThai;
                    rt = cmd.ExecuteNonQuery();
                    ts.Complete();
                    cn.Close();
                    return rt;
                }
            }
            catch
            {
                ts.Dispose();
                return 0;
            }
        }
        #endregion

        #region Update
        public static int Update(Entities.SoDuSoQuy pxh)
        {
            TransactionScope ts = null;
            int rt = 0;
            try
            {
                using (ts = new TransactionScope(TransactionScopeOption.Required))
                {
                    string sql = Common.Constants.SoQuy.UpdateSoDuSoQuy;
                    cn = DataAccess.Connection.OpenConnection();
                    SqlCommand cmd = new SqlCommand(sql, cn);
                    cmd.Parameters.Add(Common.Constants.SoQuy.MaSoDuSoQuy, SqlDbType.VarChar, 20).Value = pxh.MaSoDuSoQuy;
                    cmd.Parameters.Add(Common.Constants.SoQuy.SoDuDauKy, SqlDbType.Float).Value = pxh.SoDuDauKy;
                    rt = cmd.ExecuteNonQuery();
                    ts.Complete();
                    cn.Close();
                    return rt;
                }
            }
            catch
            {
                ts.Dispose();
                return 0;
            }
        }

        public static int UpdateTrangThai(Entities.SoDuSoQuy pxh)
        {
            int rt = 0;
            try
            {
                if (pxh.SoDuSoQuyID == 0)
                {
                    Entities.SoDuSoQuy sdsq = new Entities.SoDuSoQuy();
                    sdsq.MaSoDuSoQuy = pxh.MaSoDuSoQuy;
                    sdsq.SoDuCuoiKy = pxh.SoDuCuoiKy;
                    sdsq.SoDuDauKy = "0";
                    sdsq.TrangThai = true;
                    return Insert(sdsq);
                }
                string sql = Common.Constants.SoQuy.UpdateSoDuSoQuy;
                cn = DataAccess.Connection.OpenConnection();
                SqlCommand cmd = new SqlCommand(sql, cn);
                cmd.Parameters.Add(Common.Constants.SoQuy.SoDuSoQuyID, SqlDbType.Int).Value = pxh.SoDuSoQuyID;
                cmd.Parameters.Add(Common.Constants.SoQuy.SoDuCuoiKy, SqlDbType.Float).Value = pxh.SoDuDauKy;
                rt = cmd.ExecuteNonQuery();
                cn.Close();
                Insert(pxh);
                return rt;
            }
            catch
            {
                return 0;
            }
        }
        #endregion

        #region delete
        public static int Delete(Entities.SoDuSoQuy pxh)
        {TransactionScope ts = null;
            int rt = 0;
            try
            {
                using (ts = new TransactionScope(TransactionScopeOption.Required))
                {
                    string sql = Common.Constants.SoQuy.DeleteSoDuSoQuy;
                    SqlConnection cn = DataAccess.Connection.OpenConnection();
                    SqlCommand cmd = new SqlCommand(sql, cn);
                    cmd.Parameters.Add(Common.Constants.SoQuy.MaSoDuSoQuy, SqlDbType.VarChar, 20).Value = pxh.MaSoDuSoQuy;

                    rt = cmd.ExecuteNonQuery();
                    ts.Complete();
                    return rt;
                }
            }
            catch
            {
                ts.Dispose();
                return 0;
            }
        }
        #endregion
    }
}
#endregion